home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT68 / WGT68C.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  7.9 KB  |  370 lines

  1. #include <wgt5.h>
  2. #include <math.h>
  3.  
  4. /*
  5. ==============================================================================
  6.               WordUp Graphics Toolkit Version 5.0
  7.                  Demonstration Program 68c
  8.  
  9.  Same as wgt68b.c, only the robot is partially transparent.
  10.  
  11.  *** PROJECT ***
  12.  This program requires the WGT5_WC.LIB file to be linked.
  13.  
  14.  *** DATA FILES ***
  15.  STREET.PAL, RUN.SPR
  16.                                WATCOM C++ VERSION
  17. ==============================================================================
  18. */
  19.  
  20.  
  21. unsigned char shadowtable[256];    /* Shadow table */
  22. unsigned char *transparency_table; /* a 65536 byte table,
  23.                       allocated dynamically */
  24.  
  25. color pal[256];         /* The palette used for every graphic image */
  26. block sprites[200];     /* Array of images for the running robot */
  27.  
  28. block background;       /* Holds the background scrolling image */
  29. block work;             /* Page for constructing each frame */
  30. int oldmode;
  31.  
  32. /* A structure which holds the scrolling values for each horizontal line */
  33. typedef struct
  34.  {
  35.   int x;                /* Current x value, shifted by 8 */
  36.   int increment;        /* fixed point increment */
  37.  } line_scroll;
  38. line_scroll lines[80];  /* 80 scrolling lines along the ground */
  39.  
  40. /* A simple sprite structure */
  41. typedef struct
  42.  {
  43.   int x;
  44.   int y;
  45.   int anm;              /* Sprite number */
  46.  } sprite;
  47. sprite people[5];
  48.  
  49.  
  50. int backx = 0;          /* X value for the scrolling rocks */
  51. int backinc;            /* X increment for scrolling rocks */
  52.  
  53.  
  54.  
  55.  
  56. /* Loads the graphics files, and allocates buffers */
  57. void load_graphics (void)
  58. {
  59.  work = wallocblock (320, 200);
  60.  /* Allocate a work buffer */
  61.  
  62.  wloadsprites (pal, "run.spr", sprites, 0, 199);
  63.  background = wloadpak ("street.pak");
  64.  wsetpalette (0, 255, pal);
  65.  
  66.  transparency_table = (unsigned char *)malloc (65536);
  67. }
  68.  
  69.  
  70.  
  71. /* Frees the buffers and sprites */
  72. void free_graphics (void)
  73. {
  74.  free (transparency_table);
  75.  wfreesprites (sprites, 0, 199);
  76.  wfreeblock (background);
  77.  wfreeblock (work);
  78. }
  79.  
  80.  
  81. /* Set up the initial scrolling values */
  82. void init_lines (void)
  83. {
  84. int i;
  85. int inc;
  86.  
  87.  inc = 128;     /* slowest scrolling speed (128/256 of a pixel */
  88.  
  89.  backx = 0;     /* rocks x value */
  90.  backinc = inc; /* rocks same speed as the ground */
  91.  
  92.  for (i = 0; i < 80; i++)
  93.   {
  94.    lines[i].x = 0;              /* clear out the x value */
  95.    lines[i].increment = inc;    /* set the scroll speed */
  96.    inc += 32;                   /* Make the next row move faster */
  97.   }
  98.  
  99. }
  100.  
  101.  
  102.  
  103. /* Construct the background image */
  104. void animate_lines (void)
  105. {
  106. block source1, source2;
  107. block dest1, dest2;
  108. block origsource, origdest;
  109. int i;
  110. int x;
  111.  
  112.  wcopyscreen (0, 0, 319, 51, background, 0, 0, work);
  113.  /* Draw the moon stationary */
  114.  
  115.  
  116.  /* Scroll the rocks */
  117.  backx += backinc;
  118.  if (backx >= 81920)   /* 81920 is 320 << 8 */
  119.      backx -= 81920;
  120.  
  121.  x = backx >> 8;
  122.  
  123.  /* Copy the rocks */
  124.  wcopyscreen (x, 52, 319, 119, background, 0, 52, work);
  125.  if (x > 0)
  126.    wcopyscreen (0, 52, x-1, 119, background, 320 - x, 52, work);
  127.  
  128.  
  129.  origdest = abuf + 120 * 320;           /* First row to copy */
  130.  origsource = background + 120 * 320;   /* First row to copy */
  131.  
  132.  for (i = 0; i < 80; i++)
  133.   {
  134.    /* Scroll this line */
  135.    lines[i].x += lines[i].increment;
  136.    if (lines[i].x >= 81920)   /* 81920 is 320 << 8, wraps scroll around */
  137.       lines[i].x -= 81920;
  138.  
  139.    
  140.    x = lines[i].x >> 8;
  141.    /* Get the x coord */
  142.  
  143.    dest1 = origdest + i * 320;
  144.    dest2 = dest1 + (319 - x);
  145.    source1 = origsource + i * 320;
  146.    source2 = source1 + x;
  147.    
  148.    /* Copy the line in two steps */
  149.    memcpy (dest1, source2, 320 - x);
  150.    if (x > 0)
  151.     memcpy (dest2, source1, x + 1);
  152.    
  153.   }
  154.  
  155. }
  156.  
  157.  
  158.  
  159. /* Animates and displays the running man */
  160. void animate_man (void)
  161. {
  162.  people[0].anm++;
  163.  if (people[0].anm > 29)
  164.    people[0].anm = 0;
  165.  
  166.  wputblock_shade (people[0].x, 158, sprites[people[0].anm+30],
  167.         shadowtable, SHADE_SHADOW);
  168.  wputblock_shade (people[0].x, people[0].y, sprites[people[0].anm],
  169.         transparency_table, SHADE_TRANSLUCENT);
  170.  
  171. }
  172.  
  173.  
  174.  
  175.  
  176. void wcreate_shadow_table (color *palette)
  177. {
  178. float fr, fg, fb;
  179. long ir, ig, ib;
  180.  
  181. long absr, absg, absb;
  182.  
  183.  
  184. int r,g,b;
  185.  
  186. short col;
  187. short findcol;
  188.  
  189. unsigned long lowest;
  190. unsigned char bestfit;
  191. unsigned long coldif;
  192.  
  193.  for (col = 0; col < 256; col++)
  194.   {
  195.  
  196.    fr = (float)palette[col].r * (0.5);
  197.    fg = (float)palette[col].g * (0.5);
  198.    fb = (float)palette[col].b * (0.5);
  199.  
  200.    ir = fr;
  201.    ig = fg;
  202.    ib = fb;
  203.  
  204.    lowest = 655350;
  205.    for  (findcol = 0; findcol < 256; findcol++)
  206.     {
  207.       absr = abs ( (long)palette[findcol].r - ir);
  208.       absg = abs ( (long)palette[findcol].g - ig);
  209.       absb = abs ( (long)palette[findcol].b - ib);
  210.  
  211.       coldif = absr + absg + absb;
  212.       if  ((coldif < lowest) && (findcol != col))
  213.       {
  214.     lowest = coldif;
  215.     bestfit = findcol;
  216.       }
  217.     }
  218.    shadowtable[col] = bestfit;
  219.   }
  220.  
  221. }
  222.  
  223.  
  224. void wcreate_transparency_table (color *pal)
  225. {
  226. float lightlevel1;
  227. float lightlevel2;
  228. float fr, fg, fb;
  229. float fr2, fg2, fb2;
  230. long ir, ig, ib;
  231.  
  232. long absr, absg, absb;
  233.  
  234. short col, col2;
  235. short findcol;
  236.  
  237. unsigned long lowest;
  238. unsigned char bestfit;
  239. unsigned long coldif;
  240.  
  241.  
  242.  lightlevel1 = 0.5;     /* Percent of color 1 */
  243.  lightlevel2 = 0.5;     /* Percent of color 2 */
  244.  
  245.  /* Lightlevel1 and lightlevel2 must total to 1 */
  246.  
  247.  /* Transparency is created by taking two colors, multiplying the
  248.  RGB values by a percentage, and adding the RGB values together.  The
  249.  new color will contain a little bit of each oringal color. */
  250.  
  251.  
  252.  /* For each of the 256 colors, we can mix with any other color, therefore
  253.  we need a 256x256 table. */
  254.  
  255.  wtextcolor (15);
  256.  wtexttransparent (TEXTFGBG);
  257.  wgtprintf (0, 0, NULL, "Making transparency table...", col2);
  258.  
  259.  for (col2 = 0; col2 < 256; col2++)
  260.  {
  261.   for (col = 0; col < 256; col++)
  262.   {
  263.    fr = (float)pal[col].r * lightlevel1;
  264.    fg = (float)pal[col].g * lightlevel1;
  265.    fb = (float)pal[col].b * lightlevel1;
  266.  
  267.    fr2= (float)pal[col2].r * lightlevel2;
  268.    fg2= (float)pal[col2].g * lightlevel2;
  269.    fb2= (float)pal[col2].b * lightlevel2;
  270.  
  271.    ir = (fr + fr2);
  272.    ig = (fg + fg2);
  273.    ib = (fb + fb2);
  274.  
  275.    lowest = 655350;
  276.    for  (findcol = 0; findcol < 256; findcol++)
  277.     {
  278.       absr = abs ( (long)pal[findcol].r - ir) * 30;
  279.       absg = abs ( (long)pal[findcol].g - ig) * 59;
  280.       absb = abs ( (long)pal[findcol].b - ib) * 11;
  281.  
  282.       coldif = sqrt(absr*absr + absg*absg + absb*absb);
  283.       if  (coldif < lowest)
  284.       {
  285.     lowest = coldif;
  286.     bestfit = findcol;
  287.       }
  288.     }
  289.    transparency_table[col2 * 256L + col] = bestfit;
  290.   }
  291.  
  292.   wgtprintf (0, 8, NULL, "Color %03hi", col2);
  293.  }
  294. }
  295.  
  296.  
  297.  
  298. int load_table (char *filename, block table, int size)
  299. {
  300. FILE *in;
  301.  
  302.  in = fopen (filename, "rb");
  303.  if (in == NULL)
  304.    return 0;
  305.  fread (table, size, 1, in);
  306.  fclose (in);
  307.  return 1;
  308. }
  309.  
  310.  
  311. void save_table (char *filename, block table, int size)
  312. {
  313. FILE *out;
  314.  
  315.  out = fopen (filename, "wb");
  316.  fwrite (table, size, 1, out);
  317.  fclose (out);
  318.  
  319. }
  320.  
  321.  
  322.  
  323. void main (void)
  324. {
  325.   oldmode = wgetmode ();
  326.   if (!vgadetected ())
  327.   {
  328.     printf ("VGA is required to run this program...");
  329.     exit (1);
  330.   }
  331.  
  332.   printf ("WGT Example #68c\n\n");
  333.   printf ("This example adds transparency to the running robot.\n");
  334.   printf ("\nPress any key to begin.\n");
  335.   getch ();
  336.  
  337.   vga256 ();
  338.  
  339.  load_graphics ();
  340.  
  341.  init_lines ();
  342.  
  343.  /* Set the position of the running man */
  344.  people[0].x = 120;
  345.  people[0].y = 50;
  346.  people[0].anm = 0;
  347.  
  348.  wcreate_shadow_table (pal);
  349.  
  350.  if (!load_table ("trans.dat", transparency_table, 65536))
  351.   {
  352.    wcreate_transparency_table (pal);
  353.    save_table ("trans.dat", transparency_table, 65536);
  354.   }
  355.  
  356.  
  357.  do { 
  358.   wsetscreen (work);
  359.   animate_lines ();
  360.   animate_man ();
  361.   wnormscreen ();
  362.   wretrace ();
  363.   wputblock (0, 0, work, 0);
  364.   } while (!kbhit ());
  365.  
  366.  free_graphics ();
  367.  wsetmode (oldmode);
  368. }
  369.  
  370.